home *** CD-ROM | disk | FTP | other *** search
- /* execprnt.c --- BIBLE pp. 77-83 */
- /* ====================== PARENT ========================= */
- #include <stdio.h>
- #include <process.h>
- #include <alloc.h>
- #include <string.h>
- typedef struct TEST_DATA
- {
- char name[20];
- int n;
- double x;
- } TEST_DATA;
- /* PARENT: Test the "exec" functions. Pass address of
- * data in command line arguments as well as
- * environment variables when appropriate. */
- char *envp[] =
- {
- "PARENT=EXEC FUNCTIONS",
- NULL
- };
- main()
- {
- char *argv[4],buf[20], rname[40];
- TEST_DATA *pdata;
- /* Set up a data structure and initialize it */
- if((pdata=(TEST_DATA *)
- malloc(sizeof(TEST_DATA))) == NULL) abort();
- strcpy(pdata->name, "PARENT");
- pdata->n = 100;
- pdata->x = 1000.99;
- /* Set up the arguments for the child process */
- argv[0] = "child.exe",
- argv[1] = rname;
- sprintf(buf, "%Fp", (void far *)pdata);
- argv[2] = buf;
- argv[3] = NULL;
- /* Ask user which "exec" routine to call */
- printf("Enter name of \"exec\" function to call:");
- gets(rname);
- strlwr(rname);
- /* Call the "exec" function requested by the user */
- if(strcmp(rname, "execl") == 0)
- {
- execl("child.exe", "execl", buf, NULL);
- }
- if(strcmp(rname, "execle") == 0)
- {
- execle("child.exe",
- "child.exe", "execle", buf, NULL, envp);
- }
- if(strcmp(rname, "execlp") == 0)
- {
- execlp("child.exe",
- "child.exe", "execlp", buf, NULL);
- }
- if(strcmp(rname, "execlpe") == 0)
- {
- execlpe("child.exe",
- "child.exe", "execlpe", buf, NULL, envp);
- }
- if(strcmp(rname, "execv") == 0)
- {
- execv("child.exe", argv);
- }
- if(strcmp(rname, "execve") == 0)
- {
- execve("child.exe", argv, envp);
- }
- if(strcmp(rname, "execvp") == 0)
- {
- execvp("child.exe", argv);
- }
- if(strcmp(rname, "execvpe") == 0)
- {
- execvpe("child.exe", argv, envp);
- }
- /* Check if we could call child or not */
- if(strcmp(pdata->name, "CHILD") == 0)
- {
- printf("Back from child: name = %s, n = %d, x = %f\n",
- pdata->name, pdata->n, pdata->x);
- }
- else
- {
- printf("Don't know: %s\n", rname);
- }
- }